package com.huawei.esdk.uc.sametime.status.ext;

import org.apache.log4j.Logger;
import org.eclipse.swt.graphics.Image;

import com.huawei.esdk.uc.jna.UCService;
import com.huawei.esdk.uc.jna.util.StringUtil;
import com.huawei.esdk.uc.sametime.EsdkPluginActivator;
import com.huawei.esdk.uc.sametime.services.UCPresenceService;
import com.huawei.esdk.uc.sametime.util.PluginUtil;
import com.ibm.collaboration.realtime.people.Person;
import com.ibm.collaboration.realtime.status.ext.StatusProvider;
import com.ibm.rcp.realtime.livenames.LiveName;

/**
 * Sample mock telephony status provider.
 * 
 * @author lWX169831
 * 
 */
public class TelephonyStatusProvider implements StatusProvider
{
    private Logger logger = Logger.getLogger(TelephonyStatusProvider.class);

    public static final String TELEPHONY_STATUS_KEY = "telephony.status.key";

    private static TelephonyStatusProvider provider;

    /**
     * called when the statusProviders ext point is processed.
     */
    public TelephonyStatusProvider()
    {
        // provider = this;
        //解决CI报错问题
        TelephonyStatusProvider.initProvider(this);
    }

    public static void initProvider(TelephonyStatusProvider providerParam)
    {
        provider = providerParam;
    }

    public static TelephonyStatusProvider getInstance()
    {
        return provider;
    }

    /*
     * (non-Javadoc)
     * 
     * @see
     * com.ibm.collaboration.realtime.status.ext.StatusProvider#getStatusImage
     * (com.ibm.rcp.realtime.livenames.LiveName)
     */
    public Image getStatusImage(LiveName livename)
    {
        String ucAccount = getUCAccount(livename);
        int status = UCPresenceService.getCurrentPresence(ucAccount);

        logger.info("account = " + ucAccount + ", status = " + status
                + ", livename = " + livename);

        if (!StringUtil.isEmptyStr(ucAccount))
        {
            EsdkPluginActivator.getDefault().getPersonMap().put(ucAccount,
                    (Person) livename);

            // 订阅sametime联系人UC状态的改变通知
            UCService.getInstance().UC_SDK_SubscribeStatus(ucAccount);
        }

        return UCPresenceService.getUCStatusImage(status);
    }

    private String getUCAccount(LiveName livename)
    {
        String liveNameId = livename.getContactId();
        logger.debug("liveNameId = " + liveNameId);

        String account = "";
        if (!StringUtil.isEmptyStr(liveNameId))
        {
            account = PluginUtil.getUCAccountFromContactID(liveNameId);
        }
        return account;
    }

    /*
     * (non-Javadoc)
     * 
     * @see
     * com.ibm.collaboration.realtime.status.ext.StatusProvider#getStatusText
     * (com.ibm.rcp.realtime.livenames.LiveName)
     */
    public String getStatusText(LiveName livename)
    {
        return null;
    }
}
